<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <input type="file" name="" id="" ref="file" hidden @change="onFileChange" />
    <!-- 个人信息 -->
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isUpdateNameShow = true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isUpdateGenderShow = true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isUpdateBirthdayShow = true"
    />

    <!-- 编辑昵称 -->
    <van-popup
      style="height: 100%"
      position="bottom"
      v-model="isUpdateNameShow"
    >
      <updateName
        @close="isUpdateNameShow = false"
        v-model="user.name"
        v-if="isUpdateNameShow"
      />
    </van-popup>
    <!-- 编辑昵称 -->

    <!-- 修改性别 -->
    <van-popup position="bottom" v-model="isUpdateGenderShow">
      <updateGender
        @close="isUpdateGenderShow = false"
        v-model="user.gender"
        v-if="isUpdateGenderShow"
      />
    </van-popup>
    <!-- 修改性别 -->
    <!-- 修改出生日期 -->
    <van-popup position="bottom" v-model="isUpdateBirthdayShow">
      <updateBirthday
        @close="isUpdateBirthdayShow = false"
        v-if="isUpdateBirthdayShow"
        v-model="user.birthday"
      />
    </van-popup>
    <!-- 修改出生日期 -->
    <!-- 修改头像 -->
    <van-popup
      style="height: 100%"
      position="bottom"
      v-model="isUpdatePhotoShow"
    >
      <updatePhoto 
      :img="img" 
      @close="isUpdatePhotoShow = false" 
      v-if="isUpdatePhotoShow"
      @updatePhoto="user.photo = $event" 
      />
    </van-popup>
    <!-- 修改头像 -->
  </div>
</template>

<script>
import { getProfileList } from "@/api/login.js";
import updateName from "@/views/userProfile/components/updateName.vue";
import updateGender from "@/views/userProfile/components/updateGender.vue";
import updateBirthday from "@/views/userProfile/components/updateBirthday.vue";
import updatePhoto from "@/views/userProfile/components/updatePhoto.vue";
export default {
  data() {
    return {
      user: {}, //用户的个人资料
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false, //头像弹出层
      img: null,
    };
  },
  created() {
    this.loadProfile();
  },
  components: {
    updateName,
    updateGender,
    updateBirthday,
    updatePhoto,
  },
  methods: {
    async loadProfile() {
      try {
        const { data } = await getProfileList();
        // console.log(data);
        this.user = data.data;
      } catch (err) {
        this.$toast("数据加载失败,请稍后重试");
      }
    },
    onFileChange() {
      // 获取文件对象
      const file = this.$refs.file.files[0];
      // console.log(file);
      // 基于文章对象获取blob数据
      let binaryData = [];
      binaryData.push(file);
      const data = window.URL.createObjectURL(
        new File(binaryData, { type: "application/pdf;chartset=UTF-8" })
      );
      this.img = data;
      //解决bug,解决选择的头像是同一张,然后不弹出弹层的问题
      //把每次选择的默认值清空
      this.$refs.file.value = "";
      //图片选择好了之后,弹出弹层
      this.isUpdatePhotoShow = true;
    },
  },
};
</script>

<style scoped lang="less">
/deep/.van-nav-bar .van-icon {
  color: white;
}
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>